<?php
/**
 * ################################################
 * #【Description】 地址接口，用户小程序地址数据处理
 * #【Author】 akon
 * #【Date】 2017年8月7日
 * ################################################
 */
namespace Shx\Controller;
use Think\Controller;

class AddressController extends Controller {

    /**
     * 收货地址列表
     */
    public function addressList() {
        $receive = json_decode(file_get_contents('php://input'),true);

        if($receive['address_id']){
            M('user_address')->where(array('user_id'=>$receive['user_id'],'is_default'=>1))->setField('is_default',0);
            M('user_address')->where(array('user_id'=>$receive['user_id'],'address_id'=>$receive['address_id']))->setField('is_default',1);
        }
        $address = M('user_address')->where(array('user_id'=>$receive['user_id']))
                                    ->field('address_id,consignee,address,mobile,is_default')
                                    ->select();
        if($receive['address_id']){
            foreach($address as $key=>$val){
                if($val['is_default'] == 1){
                    $default_address = array(
                        'consignee' => $val['consignee'],
                        'address_id'=> $val['address_id'],
                        'address'   => $val['address'],
                        'mobile'    => $val['mobile']
                    );
                }
            }
        }
        if(!$address){
            $res = array(
                'status' => 0,
                'msg'    => '系统错误，请稍后重试'
            );
        }else{
            $res = array(
                'status'  => 1,
                'address' => $address,
                'default_address' => $default_address ? $default_address : ''
            );
        }
        echo json_encode($res, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
    }

    /**
     * 选择收货地址
     */
    public function selectAddress(){
        $receive = json_decode(file_get_contents('php://input'),true);
        M('user_address')->where(array('user_id'=>$receive['user_id'],'is_default'=>1))->setField('is_default',0);
        M('user_address')->where(array('user_id'=>$receive['user_id'],'address_id'=>$receive['address_id']))->setField('is_default',1);
        $address = M('user_address')->where(array('user_id'=>$receive['user_id']))->field('address_id,consignee,address,mobile,is_default')->select();
        if(!$address){
            $res = array(
                'status' => 0,
                'msg'    => '网络异常，请稍后重试'
            );
        }else{
            $res = array(
                'status' => 1,
                'address' => $address
            );
        }
        exit(json_encode($res, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
    }

    /**
     * 地区选择
     */
    public function selectArea(){
        $code = I('get.code');
        if($code == 0){
            $area = M('regions')->where(array('region_grade'=>1))->field('local_name fullName,region_id code')->select();
        }else{
            $area = M('regions')->where(array('p_region_id'=>$code))->field('local_name fullName,region_id code')->select();
        }
        echo json_encode($area,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
    }

    /**
     * 添加收货地址
     */
    public function add(){
        $receive = json_decode(file_get_contents('php://input'), true);

        $sql = "SELECT local_name FROM shx_regions WHERE
                region_id=".$receive['province']." OR
                region_id=".$receive['city']." OR
                region_id=".$receive['area'];
        $res = M()->query($sql);
        M('user_address')->where(array('user_id'=>$receive['user_id'],'is_default'=>1))->setField('is_default', 0);
        $addr_info = implode(' ',explodeArr($res, 'local_name')) . ' ' . $receive['detail'];
        $address = array(
            'user_id'    => $receive['user_id'],
            'address'    => $addr_info,
            'mobile'     => $receive['phone'],
            'consignee'  => $receive['name'],
            'is_default' => 1,
            'create_time'=> date('Y-m-d H:i:s', time())
        );
        $id = M('user_address')->add($address);
        if($id){
            $res = array(
                'status'     => 1,
                'address_id' => $id,
                'address'    => $addr_info
            );
        }else{
            $res = array(
                'status' => 0,
                'msg'    => '系统错误，请稍后重试'
            );
        }
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }

}